jQuery(document).ready(function(){
    // CSS additions for style (using document.ready so the css styles 
	// will be available when the window.load function is executed).
    stringForStyles = '<style type="text/css">';
    //CSS for the "back to top" button
    stringForStyles += 'a.backToTop:link, a.backToTop:visited {padding:0 10px 0 10px; font-size: 1em; color:white !important; background-color:#151515; display:block; text-decoration:none; width 15px; height: 40px; line-height: 40px; position:fixed; bottom:10px; right:15px; z-index:1666 !important; opacity:0.4; border-radius:5px;} ';
    stringForStyles += 'a.backToTop:hover {background-color:#08088A;}';
    //CSS for nav menu links
    stringForStyles += '.ws-theme-sidebar-inner a:link {color:#000000; text-decoration:none;} ';
    stringForStyles += '.ws-theme-sidebar-inner a:visited {color:#000000;}';
    stringForStyles += '.ws-theme-sidebar-inner a:hover {color:#0404B4; text-decoration:underline;}'; //0B0B61//0101DF

    stringForStyles += '</style>';

    jQuery('head').append(stringForStyles);
});

jQuery(window).load(function(){
	var dragging = false;
	var starting_drag_coordX; 
	var starting_drag_coordY; 
	var offsetX;
	var offsetY;
 
jQuery('.wikiPage img').each(function() {
	
	if (jQuery(this).hasClass('thumbnail')) {
     if (jQuery(this).width()<jQuery(this).height()){
         jQuery(this).width(150);
     } else {
         jQuery(this).height(150); 
     };
      	}

	jQuery(this).css('position', 'relative');
//  jQuery(this).parent().css('text-align', 'left');
	jQuery(this).wrap( "<div style='position:relative;'></div>" );

	// stores the original width, top, and left of the images so when you hover off, it goes back to the original values
	jQuery.data(this, 'width', jQuery(this).width());
	jQuery.data(this, 'top', jQuery(this).position().top);
	jQuery.data(this, 'left', jQuery(this).position().left);
	// event listeners for the scroll zoom effect        
	if (this.addEventListener) {
		// IE9, Chrome, Safari, Opera
		this.addEventListener("mousewheel", MouseWheelHandler, false);
		// Firefox
		this.addEventListener("DOMMouseScroll", MouseWheelHandler, false);
	}
	
	this.addEventListener('mousedown',startDrag, false);
    this.addEventListener('mouseup',stopDrag, false);
});

    // called when user clicks and holds on an image
    function startDrag(e){
        dragging = true;
        offsetX = e.clientX;          //clientX event attribute returns the horizontal coordinate (according to the client area) of the mouse pointer when an event was triggered
        offsetY = e.clientY;
        starting_drag_coordX = jQuery(this).position().left //- jQuery(this).parent().position().left;
        starting_drag_coordY = jQuery(this).position().top //- jQuery(this).parent().position().top;
        document.onmousemove = mouseIsMoving;
     };
 
     //called when user is moving mouse over image
     //only moves image when dragging, i.e., when mouse button is down
    function mouseIsMoving(e) {
        if (!dragging){return};
        if (!e) { var e= window.event};
        var targ = e.target ? e.target : e.srcElement;
        targ.style.left=starting_drag_coordX + e.clientX - offsetX - jQuery(targ).data('left') + jQuery(targ).data('left') * 0.05 + 'px';
//		console.log("jQuery(this).data('left') = " + jQuery(targ).data('left') + " // starting_drag_coordX = " + starting_drag_coordX + " // e.clientX = " + e.clientX + " //offsetX = " + offsetX + " // targ.style.left =" + targ.style.left);
        targ.style.top=starting_drag_coordY + e.clientY - offsetY + 'px';
		return false;
        };
        
   // stops the image from dragging (otherwise, it would be stuck to cursor)
    function stopDrag(){
        dragging = false;
    }; 

    var original_width_for_image_resize;
    var content_box_for_image_resize;
    var new_width_for_image_resize;
    var enlarge_factor;

    jQuery('.wikiPage img').hover(
        function(){
			original_width_for_image_resize = jQuery(this).data('width');
            content_box_for_image_resize = jQuery('.contentBox').width();
			if (jQuery(this).hasClass('thumbnail')){
				enlarge_factor = 4.5;
			}else if (original_width_for_image_resize < 400){
                enlarge_factor = 2.0;
			}else if (original_width_for_image_resize > 1200){
                enlarge_factor = 1.0;
			} else {
                enlarge_factor = 2.0 - ( original_width_for_image_resize / 1200 );
			};

            new_width_for_image_resize = Math.max(original_width_for_image_resize, Math.min(content_box_for_image_resize, Math.min(1200, enlarge_factor * original_width_for_image_resize)));
            jQuery(this).stop().animate({
				width: new_width_for_image_resize + 'px',
                height: 'auto'
            }, 300);
        },
        function(){
            jQuery(this).stop().animate({
				width: jQuery(this).data('width') + 'px',
				height: 'auto',        
                top: 0,
                left: 0 
            }, 300);
        }
    );

    function MouseWheelHandler(e) {
        // cross-browser wheel delta
        var e = window.event || e; // old IE support
        if (e.preventDefault) 
        { 
			e.preventDefault(); 
        } else { 
            e.returnValue = false; //for IE
        } 
		var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
        jQuery(this).width ( Math.max(50, Math.min(3000, jQuery(this).width() + (50 * delta))) + "px");
		return false;
    }

    // this adds a custom mouse cursor when the image is moused over (gives user hint that image can be scrolled to zoom)
    var stringForStyles = '<style type="text/css">';
      stringForStyles += ".wikiPage img { cursor:url('https://entresources2.googlecode.com/svn/magnifying_glass_3.png'),url('https://entresources2.googlecode.com/svn/magnifying_glass_3.cur'),crosshair; max-width: 1200px;}";
    stringForStyles += '</style>';
    jQuery('head').append(stringForStyles);

    // ADDS A BACK TO TOP BUTTON ON EACH PAGE
	var html_to_append = '<a class="backToTop" href="#TopOfPage" rel="nofollow">Back to Top</a>';
	jQuery('body').append(html_to_append);
    html_to_append = '<div name="TopOfPage" id="TopOfPage"></div>';
    jQuery('body').prepend(html_to_append);
    
// ------------------------------------------------------------------------------------------------------
// ADDS FUNCTIONALITY TO INSERT A WIDGET WITH CLASS "toggle_answer" AND ONCLICK="toggle_answer(event)"
    var stringForStyles = '<style type="text/css">'+
		'.toggle_answer {' +
			'border-color:gray;' +
			'border-width:1px;' + 
			'border-style:solid;' +
			'border-radius:5px;' +
			'background-color:#D8D8D8;' +
			'background-image:url(http://entinfo.org/images/question_mark2.png);' +
			'background-position:center;' +
			'background-repeat:no-repeat;' +
			'background-size:30px 30px;' +
      'padding: 8px 10px 8px 10px;' +
      'margin: 4px 0 0 0;' +
			'min-height:4em;' +
			'cursor:pointer;}' +
		'</style>';
    jQuery('head').append(stringForStyles);
      
    jQuery('.toggle_answer').on('click', function(){
    //  function toggle_answer(event){
       if ( jQuery(this).children().css('opacity') == 1 ) 
       {
          jQuery(this).find('*').fadeTo(0, 0);
          jQuery(this).css({	"border-color": "gray", 
                            "border-width":"1px", 
                            "border-style":"solid",
                            "background-color":"#D8D8D8",
                            "background-image":"url(http://entinfo.org/images/question_mark2.png)"});
       } else 
       {
          jQuery(this).find('*').fadeTo(0, 1);
          jQuery(this).css({	"border-color": "#D8D8D8", 
                            "border-width":"1px", 
                            "border-style":"solid",
                            "background-color":"#FAFAFA",
                            "background-image":"none"});
       };
       
      });
  
  
// ------------------------------------------------------------------------------------------------------


// ------------------------------------------------------------------------------------------------------
// ADDS FORMATTING FOR THE "ASIDE BOXES"
    stringForStyles = 
        '<style type="text/css">' +
            '.aside_box{'+
               'border: 1px solid gray;'+
               'border-radius: 8px;'+
               'background-color: #FAFAFA;'+
               'width: 80%;'+
               'margin: 10px 10% 0 10%;'+
               'min-width: 400px;' +
               'padding: 17px 5px 12px 5px;' +
            '}'+
            
          '.aside_box_title {' +
               'border: 1px solid gray;' +
               'border-radius: 6px;' +
               'background-color: #E6E6E6;' +
               'padding: 10px 15px 10px 15px; ' +
               'margin-bottom: 10px;'+
               'font-weight: bold;' +
               'font-size: 19px; ' +
               'display: inline-block;' +
          '}' +         
           
        '</style>';

    jQuery('head').append(stringForStyles);
// ------------------------------------------------------------------------------------------------------

});
